package com.zg.nacos.web;

import cn.hutool.core.util.StrUtil;
import com.zg.common.core.R;
import com.zg.nacos.domain.req.RoleEditReq;
import com.zg.nacos.domain.req.RolePageReq;
import com.zg.nacos.domain.req.UserEditReq;
import com.zg.nacos.util.NacosUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriBuilder;
import reactor.core.publisher.Mono;

import javax.validation.Valid;
import javax.websocket.server.PathParam;
import java.time.Duration;
import java.util.Map;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/6/20 16:11
 */

@RestController
@RequestMapping("/nacos")
@Api(tags = "nacos服务")
@Slf4j
public class NacosRoleController {



    @Autowired
    private WebClient webClient;

    @ApiOperation(value="角色列表")
    @PostMapping("/role/page")
    public R pageRole(@Valid  @RequestBody RolePageReq userPageReq) {
        return webClient.get()
                .uri(uriBuilder -> {
                    UriBuilder builder = uriBuilder.path("/nacos/v1/auth/roles");
                    // 仅当参数非空时才添加 queryParam
                    if (!StrUtil.isEmpty(userPageReq.getRole())) {
                        builder.queryParam("role",userPageReq.getRole());
                    }
                    if (!StrUtil.isEmpty(userPageReq.getUsername())) {
                        builder.queryParam("username",userPageReq.getUsername());
                    }
                    builder.queryParam("pageSize", userPageReq.getPageSize());
                    builder.queryParam("pageNo", userPageReq.getPageNo());
                    builder.queryParam("search", "accurate");
                    builder.queryParam("accessToken",userPageReq.getNacosToken());
                    return builder.build();
                })
                .retrieve()
                .bodyToMono(Map.class)
                .map(response -> NacosUtils.genPage(response, userPageReq.getPageNo(),userPageReq.getPageSize()))
                .block(Duration.ofSeconds(5));
    }

    @ApiOperation(value="角色新增")
    @PostMapping("/role/add")
    public R addRole(@Valid @RequestBody RoleEditReq editReq) {
        MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();

        // 使用add方法添加表单字段（更适合multipart/form-data）
        formData.add("username", editReq.getUsername());
        formData.add("role", editReq.getRole());

            Map map= webClient.post()
                    .uri(uriBuilder -> {
                        UriBuilder builder = uriBuilder.path("/nacos/v1/auth/roles");
                        builder.queryParam("accessToken",editReq.getNacosToken());  // 仅当参数非空时才添加 queryParam
                        return builder.build();
                    })
                    .contentType(MediaType.MULTIPART_FORM_DATA)
                    .body(BodyInserters.fromMultipartData(formData))
                    .retrieve()
                    .bodyToMono(Map.class)
                    .block(Duration.ofSeconds(5));
            return NacosUtils.handleResponse(map);


    }

    @ApiOperation(value="角色删除")
    @PostMapping("/role/remove")
    public R removeRole(@Valid @RequestBody RoleEditReq editReq) {

            Map map= webClient.delete()
                    .uri(uriBuilder -> {
                        UriBuilder builder = uriBuilder.path("/nacos/v1/auth/roles");
                        builder.queryParam("username",editReq.getUsername());
                        builder.queryParam("role",editReq.getRole());
                        builder.queryParam("accessToken",editReq.getNacosToken());
                        // 仅当参数非空时才添加 queryParam
                        return builder.build();
                    })
                    .retrieve()
                    .bodyToMono(Map.class)
                    .block(Duration.ofSeconds(5));
            return NacosUtils.handleResponse(map);

    }



}